package ex.datastream.physicalPartition;


import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import org.apache.flink.api.common.functions.MapFunction;

public class Rebalance {
    public static void main(String[] args) throws Exception {
        // 获取执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        // 构建数据流
        DataStream<Integer> input = env.fromElements(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

        // 对数据流进行均匀分区
        DataStream<Integer> rebalancedStream = input.rebalance();

        // 对分区后的数据流进行map操作
        DataStream<Integer> mappedStream = rebalancedStream.map(new MapFunction<Integer, Integer>() {
            @Override
            public Integer map(Integer value) throws Exception {
                return value ;
            }
        });


        // 打印分区后的数据流
        mappedStream.print();

        // 执行任务
        env.execute("Rebalance Example");
    }
}